//
//  BaseInfoCell.swift
//  SBSafetyApp
//
//  Created by Aimeow on 2022/11/19.
//

import Foundation
import UIKit


enum BaseInfoStyleEnum: Int {
    // https://pic4.58cdn.com.cn/nowater/webim/big/n_v2e7c11c6f10a245519118f4f1774d6798.png
    case Normal = 1001
    // https://pic2.58cdn.com.cn/nowater/webim/big/n_v291338bf2bf144a80ac6d2d6aaf1b4501.png
    case Style1 = 1002
}

enum BaseInfoStatesEnum: String {
    case Error = "0"
    case Normal = "1"
    case UnFinish = "2"
}

class BaseInfoModel: NSObject {
    var style: BaseInfoStyleEnum
    var title: String
    var detail: String?
    var msg: String?

    var status: MyItemStatus? = .single

    init(style: BaseInfoStyleEnum, title: String, detail: String? = nil, msg: String? = nil, status: MyItemStatus? = nil) {
        self.style = style
        self.title = title
        self.detail = detail
        self.msg = msg
        self.status = status
    }
}

class BaseInfoCell: UITableViewCell {
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        return label
    }()

    lazy var detailLabel: UILabel = {
        let label = UILabel()
        return label
    }()
    
    lazy var msgLabel: UILabel = {
        let label = UILabel()
        label.textColor = .lightGray
        label.font = .systemFont(ofSize: 13)
        return label
    }()

    lazy var indicateImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage.init(systemName: "chevron.right"))
        return imageView
    }()

    lazy var line: UIView = {
        let l = UIView()
        l.backgroundColor = .hex("#F3F3F3")
        return l
    }()
    
    lazy var baseV: CornerView = {
        let v = CornerView()
        return v
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .white
        setupUI()
    }

    func buildData(model: BaseInfoModel, status: BaseInfoStatesEnum.RawValue?) {

        titleLabel.text = model.title
        detailLabel.text = model.detail ?? "--"

        switch (model.style) {
        case BaseInfoStyleEnum.Normal:
            titleLabel.textColor = UIColor.hex("#666666")
            titleLabel.font = UIFont.systemFont(ofSize: 15)

            detailLabel.textColor = UIColor.hex("#000000")
            detailLabel.font = UIFont.systemFont(ofSize: 16)
            break;
        case BaseInfoStyleEnum.Style1:

            titleLabel.textColor = UIColor.hex("#333333")
            titleLabel.font = UIFont.systemFont(ofSize: 17)
            
            msgLabel.text = model.msg

            if let taskStatus = status {
                if (taskStatus == BaseInfoStatesEnum.UnFinish.rawValue) {
                    detailLabel.textColor = UIColor.hex("#FF0000")
//                    detailLabel.text = "未完成"
                    detailLabel.text = ""
                } else if taskStatus == BaseInfoStatesEnum.Normal.rawValue {
                    detailLabel.textColor = UIColor.hex("#0FA404")
                    detailLabel.text = "正常"
                } else {
                    detailLabel.textColor = UIColor.hex("#FF0000")
                    detailLabel.text = "异常"
                }
            }

            detailLabel.font = UIFont.systemFont(ofSize: 15)
            break;
        }
        
        if model.msg == nil || model.msg == "" {
            titleLabel.snp.remakeConstraints { make in
                make.top.equalToSuperview().offset(16)
                make.bottom.equalToSuperview().offset(-16)
                make.left.equalTo(16)
                make.width.equalTo(ScreenWidth - 120)
            }
            
            msgLabel.snp.removeConstraints()
        } else {
            titleLabel.snp.remakeConstraints { make in
                make.top.equalToSuperview().offset(16)
                make.left.equalTo(16)
                make.width.equalTo(ScreenWidth - 120)
            }
            
            msgLabel.snp.remakeConstraints { make in
                make.top.equalTo(titleLabel.snp.bottom).offset(10)
                make.bottom.equalToSuperview().offset(-16)
                make.left.equalTo(16)
                make.width.equalTo(ScreenWidth - 120)
            }
        }

        switch model.status {
        case .single:
            baseV.corners = SBRectCorner(all: 10)
            break
        case .top:
            baseV.corners = SBRectCorner(topLeft: 10, topRight: 10)
            break
        case .middle:
            baseV.corners = SBRectCorner(all: 0)
            break
        case .bottom:
            baseV.corners = SBRectCorner(bottomLeft: 10, bottomRight: 10)
            break
        case .none:
            baseV.corners = SBRectCorner(all: 0)
            break
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        baseV.backgroundColor = .white
        contentView.addSubview(baseV)
        baseV.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
        }

        baseV.addSubview(detailLabel)
        detailLabel.textAlignment = .right
        detailLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-16)
        }

        baseV.addSubview(titleLabel)
        titleLabel.numberOfLines = 0
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(16)
            make.left.equalTo(16)
            make.width.equalTo(ScreenWidth - 120)
        }
        
        baseV.addSubview(msgLabel)
        msgLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(-16)
            make.left.equalTo(16)
            make.width.equalTo(ScreenWidth - 120)
        }
        
        baseV.addSubview(line)
        line.snp.makeConstraints { (make) -> Void in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.height.equalTo(0.5)
        }
    }

}
